//
//  WLRouterDeviceCell.m
//  WanLian
//
//  Created by muzhuang on 2018/10/17.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLRouterDeviceCell.h"

@implementation WLRouterDeviceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _ivHead = [[UIImageView alloc] initWithFrame:CGRectMake(16, (WLScaleX(74)-38)/2.0, 46, 38)];
        _ivHead.image = [UIImage imageNamed:@"router_device"];
        [self.contentView addSubview:_ivHead];
        
        _labTitle = [[UILabel alloc] init];
        _labTitle.font = [WLCommonTool fontTypePingFangSCMedium:17];
        _labTitle.textColor = wlBlackColor;
        [self.contentView addSubview:_labTitle];
        
        _labDeviceMac = [[UILabel alloc] init];
        _labDeviceMac.textAlignment = NSTextAlignmentCenter;
        _labDeviceMac.font = [WLCommonTool fontTypePingFangSCRegular:11];
        _labDeviceMac.textColor = wlDarkGrayColor;
        [self.contentView addSubview:_labDeviceMac];
        
        _labStatus = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_labTitle.frame)+2, _labTitle.frame.origin.y, 30, 24)];
        _labStatus.font = [WLCommonTool fontTypePingFangSCMedium:12];
        _labStatus.textColor = wlGreenColor;//wlLightTextColor;
        _labStatus.text = @"在线";
        _labStatus.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_labStatus];
        
        _labBlack = [[UILabel alloc] init];
        _labBlack.backgroundColor = wlDarkGrayColor;
        _labBlack.font = [WLCommonTool fontTypePingFangSCRegular:8];
        _labBlack.textColor = [UIColor whiteColor];
        _labBlack.text = @"黑名单";
        _labBlack.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_labBlack];
        _labBlack.layer.cornerRadius = 2;
        _labBlack.layer.masksToBounds = YES;
        /*
        _labUpSpeed = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-16-50, _labDeviceMac.frame.origin.y, 50, 20)];
        _labUpSpeed.textAlignment = NSTextAlignmentRight;
        _labUpSpeed.font = [WLCommonTool fontTypePingFangSCMedium:11];
        _labUpSpeed.textColor = wlLightTextColor;
//        _labUpSpeed.text = @"0Kb/s";
        [self.contentView addSubview:_labUpSpeed];
        
        _vUpTip = [[UIView alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-16-35-WLScaleX(6), CGRectGetMidY(_labUpSpeed.frame)-WLScaleX(3), WLScaleX(6), WLScaleX(6))];//WIDTH_SCREEN_WL-16-50-WLScaleX(6)
        _vUpTip.backgroundColor = wlYellowColor;
        [self.contentView addSubview:_vUpTip];
        _vUpTip.layer.cornerRadius = WLScaleX(3);
        
        _labDownSpeed = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-16-100-WLScaleX(6), _labDeviceMac.frame.origin.y, 50, 20)];
        _labDownSpeed.font = [WLCommonTool fontTypePingFangSCMedium:11];
        _labDownSpeed.textColor = wlLightTextColor;
//        _labDownSpeed.text = @"1980Kb/s";
        [self.contentView addSubview:_labDownSpeed];
        
        _vDownTip = [[UIView alloc] initWithFrame:CGRectMake(WIDTH_SCREEN_WL-16-100-2-WLScaleX(12), CGRectGetMidY(_labDownSpeed.frame)-WLScaleX(3), WLScaleX(6), WLScaleX(6))];
        _vDownTip.backgroundColor = wlUIColorRGB(0x22FFCF);
        [self.contentView addSubview:_vDownTip];
        _vDownTip.layer.cornerRadius = WLScaleX(3);
        */
    }
    return self;
}

- (void)setRouterDevices:(WLRouterDeviceModel *)routerDevices
{
    _routerDevices = routerDevices;
    _labTitle.text = routerDevices.name;
    _labDeviceMac.text = [NSString stringWithFormat:@"MAC:%@",routerDevices.macAddress];
//    _labStatus.text = @"在线";
    if (routerDevices.access) {
        _labBlack.alpha = 0;
    }
    else
    {
        _labBlack.alpha = 1.0;
    }
    /*
    if (routerDevices.upAndDownSize != nil)
    {
//        NSData *jsonData = [routerDevices.upAndDownSize dataUsingEncoding:NSUTF8StringEncoding];
//        NSError *err;
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
//                                                            options:NSJSONReadingMutableContainers
//                                                              error:&err];
//        _labUpSpeed.text = [NSString stringWithFormat:@"%@",routerDevices.upAndDownSize[@"up"]];
//        _labDownSpeed.text = [NSString stringWithFormat:@"%@",routerDevices.upAndDownSize[@"down"]];
//        _labUpSpeed.text = [NSString stringWithFormat:@"%@Kb/s",routerDevices.upAndDownSize.up];
//        _labDownSpeed.text = [NSString stringWithFormat:@"%@Kb/s",routerDevices.upAndDownSize.down];
        
        float upSpeed = [routerDevices.upAndDownSize.up integerValue]/1024.0;
        NSString *strUp = [NSString stringWithFormat:@"%.fKb/s",upSpeed];
        if (upSpeed>1024)
        {
            strUp = [NSString stringWithFormat:@"%.fMb/s",upSpeed/1024.0];
        }
        _labUpSpeed.text = strUp;
        
        float downSpeed = [routerDevices.upAndDownSize.down integerValue]/1024.0;
        NSString *strDown = [NSString stringWithFormat:@"%.fKb/s",downSpeed];
        if (downSpeed>1024) {
            strDown = [NSString stringWithFormat:@"%.fMb/s",downSpeed/1024.0];
        }
        _labDownSpeed.text = strDown;
    }
    */
    [self layoutIfNeeded];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    WLWkSelf
//    [_ivHead mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.contentView.mas_left).offset(WLScaleX(16));
//        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(50, 50));
//    }];
    
    CGFloat widthTitle;
    if (_routerDevices.access) {
        CGSize size = [_labTitle.text boundingRectWithSize:CGSizeMake(WIDTH_SCREEN_WL, 24) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [WLCommonTool fontTypePingFangSCMedium:17]} context:nil].size;
        if (size.width>=WIDTH_SCREEN_WL-16-46-10-32-15)
        {
            widthTitle = WIDTH_SCREEN_WL-16-46-10-32-15+2;
        }
        else
        {
            widthTitle = size.width+2;
        }
    }
    else
    {
        CGSize size = [_labTitle.text boundingRectWithSize:CGSizeMake(WIDTH_SCREEN_WL, 24) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [WLCommonTool fontTypePingFangSCMedium:17]} context:nil].size;
        
        if (size.width>=WIDTH_SCREEN_WL-16-46-10-32-36-15)
        {
            widthTitle = WIDTH_SCREEN_WL-16-46-10-32-36-15+2;
        }
        else
        {
            widthTitle = size.width+2;
        }
        
        [_labBlack mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.labStatus.mas_right);//.offset(5);
            make.size.mas_equalTo(CGSizeMake(36, 12));
            make.centerY.equalTo(weakSelf.labStatus.mas_centerY);
        }];
    }
    
    [_labTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.ivHead.mas_right).offset(10);//(WLScaleX(12));
        make.top.equalTo(weakSelf.ivHead.mas_top).offset(-3);
        make.width.equalTo(@(widthTitle));
        make.height.equalTo(@(24));
    }];
    
    [_labStatus mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.labTitle.mas_right);//.offset(WLScaleX(10));
        make.top.equalTo(weakSelf.labTitle.mas_top);
//        make.height.equalTo(weakSelf.labTitle.mas_height);
        make.size.mas_equalTo(CGSizeMake(32, 24));
    }];
    
//    [_labBlack mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.labStatus.mas_right);//.offset(5);
//        make.size.mas_equalTo(CGSizeMake(36, 12));
//        make.centerY.equalTo(weakSelf.labStatus.mas_centerY);
//    }];
    
    [_labDeviceMac mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.labTitle.mas_left);
        make.top.equalTo(weakSelf.labTitle.mas_bottom);
        make.height.equalTo(@(20));
    }];
    /*
    [_labUpSpeed mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-WLScaleX(16));
//        make.centerY.equalTo(weakSelf.labDeviceMac.mas_centerY);
        make.top.equalTo(weakSelf.labDeviceMac.mas_top);
        make.height.equalTo(@(20));
    }];
    
    [_vUpTip mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.labUpSpeed.mas_left).offset(-WLScaleX(4));
        make.centerY.equalTo(weakSelf.labUpSpeed.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(WLScaleX(6), WLScaleX(6)));
    }];
    
    [_labDownSpeed mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.vUpTip.mas_left).offset(-WLScaleX(12));
        make.top.equalTo(weakSelf.labDeviceMac.mas_top);
        make.height.equalTo(@(20));
    }];
    
    [_vDownTip mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.labDownSpeed.mas_left).offset(-WLScaleX(4));
        make.centerY.equalTo(weakSelf.labUpSpeed.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(WLScaleX(6), WLScaleX(6)));
    }];
    */
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
